//
//  ViewController.m
//  sdafasdf
//
//  Created by Rocky on 16/4/25.
//  Copyright © 2016年 Rocky. All rights reserved.
// 参考 http://blog.csdn.net/lizhongfu2013/article/details/9497187

#import "ViewController.h"
#import <objc/runtime.h>
#import <objc/message.h>
#import "TestView.h"
//
@interface ViewController ()

@end
const NSString* Key = @"ViewController.Key";
//
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //一样的功能
    objc_msgSend(self,@selector(makeText));
    objc_msgSend(self, sel_registerName("makeText2:"),@"info");
    [self makeText];
    //动态加载类
    const char *className = "TestView";
    Class theClass = objc_getClass(className);
    id view = [[theClass alloc]init];
    NSLog(@"====view=====%@",view);
    //替换函数
    SEL  targetSelector = @selector(makeText);
    class_replaceMethod([self class], targetSelector, (IMP)demoReplaceMethod,"v@:");
    [self makeText];//已被替换了
    //objc_msgSend(self,@selector(makeText));
    //
    for (int i =0 ; i<5; i++)
    {
        UIView* view = [[ UIView alloc]initWithFrame:CGRectMake(20+40*i, 100, 30, 30)];
        [view setBackgroundColor:[UIColor greenColor]];
        UITapGestureRecognizer* touch = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(view_touch:)];
        objc_setAssociatedObject(touch, (__bridge const void *)(Key),[NSString stringWithFormat: @"我是被挂载的:%d",i], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [view addGestureRecognizer:touch];
        [self.view addSubview:view];
    }
    [self getClassAllMethod];
    [self propertyNameList];
}

//获取一个类的所有方法s
- (void) getClassAllMethod
{
    u_int count;
    Method* methods= class_copyMethodList([TestView class], &count);
    for (int i = 0; i < count ; i++)
    {
        SEL name = method_getName(methods[i]);
        NSString *strName = [NSString stringWithCString:sel_getName(name)encoding:NSUTF8StringEncoding];
        NSLog(@"method_getName==%@",strName);
    }
}
//获取一个类的所有属性
- (void) propertyNameList
{
    u_int count;
    objc_property_t *properties=class_copyPropertyList([TestView class], &count);
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName =property_getName(properties[i]);
        NSString *strName = [NSString stringWithCString:propertyName encoding:NSUTF8StringEncoding];
        NSLog(@"propertyName===：%@",strName);
        
    }
    
}
//
void demoReplaceMethod(id SELF, SEL _cmd)
{
    NSLog(@"==demoReplaceMethod ====%s",sel_getName(_cmd));
}
-(void)view_touch:(UITapGestureRecognizer*)sender
{
    //[sender.view removeGestureRecognizer:sender];
    NSString *str = objc_getAssociatedObject(sender, (__bridge const void *)(Key));
    NSLog(@"%@",str);
    //objc_removeAssociatedObjects(sender);
}

-(void)makeText
{
    NSLog(@"====makeText=====");
    
}
-(void)makeText2:(NSString*)str
{
    NSLog(@"====makeText2=====%@",str);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
